package com.sangmo.fts.process.factoring.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.common.utils.DateUtils;
import com.sangmo.boot.framework.data.idm.IdentityManager;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.factoring.interfaces.FfFactoringContractCreation;
import com.sangmo.fts.process.factoring.model.FfFactoringContract;
import com.sangmo.fts.process.factoring.model.FfLoan;
import com.sangmo.fts.process.factoring.repo.FfFactoringContractRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
@Transactional
public class FfFactoringContractEntityService extends AuthorityEntityService<FfFactoringContract> {

    @Autowired
    private FfFactoringContractRepo ffFactoringContractRepo;

    @Autowired
    private IdentityManager identityManager;

    @Autowired
    private FfLoanService ffLoanService;

    @Autowired
    private FileUploadService fileUploadService;

    @Override
    protected IDataViewRepo<FfFactoringContract> getRepo() {
        return ffFactoringContractRepo;
    }

    @Override
    protected void onCreate(FfFactoringContract entity, Object bo) {
        super.onCreate(entity, bo);
        entity.setCode(DateUtils.format(new Date(), "yyyyMMdd") + "3" + identityManager.next("FF_FACTORING_CONTRACT", 5));
        FfFactoringContractCreation creation = (FfFactoringContractCreation) bo;
        FfLoan loan = ffLoanService.get(creation.getLoanId());
        entity.setContractId(loan.getContractId());
        entity.setFinancingId(loan.getApplyId());
        entity.setBankId(loan.getBankId());
        entity.setFeeRate(new BigDecimal("0.005"));
        entity.setDueDate(loan.getEndTime());
    }

    @Override
    protected void afterCreated(FfFactoringContract entity, Object bo) {
        super.afterCreated(entity, bo);
        FfFactoringContractCreation creation = (FfFactoringContractCreation) bo;
        if (null != creation.getBankConfirmDoc()) {
            fileUploadService.updateRef(creation.getBankConfirmDoc(), ""+entity.getId(), "FfFactoringContract.Bank");
        }
    }

    @Override
    protected void onDelete(FfFactoringContract entity) {
        super.onDelete(entity);
        if (entity.isBankConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ffFactoringContract.remove.permit.disabled");
        }
    }

    public void bankConfirm(FfFactoringContract contract) {
        contract.setBankConfirm(true);
        this.save(contract);
    }

    public void sellerConfirm(FfFactoringContract contract) {
        contract.setSellerConfirm(true);
        this.save(contract);
    }

    protected FfFactoringContract findAvailableById(Long id, Long trainingId) {
        FfFactoringContract ffFactoringContract = ffFactoringContractRepo.findAvailableById(id, trainingId);
        if (null == ffFactoringContract) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ffFactoringContract.not.available");
        }
        return ffFactoringContract;
    }
}
